<?php

require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_COST_DM_PRICE.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_COST_DM,$login['staff_auth']);
$smarty->assign('now_menu', $aokimenu[AOKI_AUTH_COST_DM]["li"]);
$login_auth = $login['staff_auth'];
$smarty->assign('login_auth',$login_auth);

$types = Class_M_OTHER::get_dmtype_array();
$dosearch = FALSE ;
$dao = new Class_M_COST_DM_PRICE;

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	 $forms =  $_SESSION['MSEARCH']['COST_DM_PRIC'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['COST_DM_PRIC'] = $forms;
}

if (empty($forms["year4"])) {
	$forms["year4"] = date('Y');
}
$wherearr =  array();

$wherearr[] = sprintf(" year4 =  '%s' ",  $forms['year4'] );



//---CSVダウンロード　処理開始

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "dm_price".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	
	$colums = array();
	$col_heads = array(
		'year4'         => array( '年度',                   1),
		'num_from'      => array( '通数範囲（下限）	整数',  1),
		'num_to'        => array( '通数範囲（上限）	整数',  1)
	);
	foreach ($types as $kk => $vv ) {
		$col_heads["type".$kk] = array($vv,1) ; 
	}
	
//$page内容は関数内部更新されます。


	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	//データ整形開始
	$data = $dao->search( $wherearr, $page );
	$dataSub = $dao->searchSub( $wherearr );
	//表示ために整形開始
	foreach ($data as $k => $v ) {
		$pid = $v["id"];
		foreach ($types as $kk => $vv ) {
			$data[$k]["type".$kk] = $dataSub[$pid][$kk] ; 
		}
	}
	//データ整形完了
	$dao->output_csv($data, $colums);
	exit;
}


//---CSVダウンロード　処理完了
$dosearch = TRUE;
if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
	$dosearch = TRUE;
}

if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
	$dosearch = TRUE;
	$page['current'] = $forms['page'];
}else{
	$page['current'] = 1;
}
$page['item'] = PAGE_MAX;

if ($dosearch) {
	//$page内容は関数内部更新されます。
	$data = $dao->search( $wherearr, $page );
	$dataSub = $dao->searchSub( $wherearr );
	//表示ために整形開始
	foreach ($data as $k => $v ) {
		$pid = $v["id"];
		
		foreach ($types as $kk => $vv ) {
			
			$data[$k]["type".$kk] = $dataSub[$pid][$kk] ; 
		}
	}

}
//表示ために整形完了
/*
$electrics = Class_M_OTHER::get_electric_array(PLEASE_SELECT_ARR);
$smarty->assign('electrics', $electrics );
*/


$smarty->assign('year4',  Class_PWD::get_years());
$smarty->assign('tmtypes', $types );

	
if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) )  $smarty->assign('data', $data);
//if( isset($disp) )  $smarty->assign('disp', $disp);
if( isset($page) )  $smarty->assign('page', $page);

$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('mcost_dm_price.html');
?>